import Base64 from 'lib/base64'
// 判断当前是否是安卓手机
export const is_android = () => {
  var ua = navigator.userAgent.toLocaleLowerCase()
  if (ua.indexOf('android') > -1 || ua.indexOf('linux') > -1 || ua.indexOf('Android') > -1) {
    return true
  } else {
    return false
  }
}
// 判断当前是否是iphone手机
export const is_iphone = () => {
  var ua = navigator.userAgent.toLocaleLowerCase()
  if (ua.indexOf('iphone') > -1 || ua.indexOf('ipad') > -1 || ua.indexOf('ipod') > -1 || ua.indexOf('macintosh') > -1) {
    return true
  } else {
    return false
  }
}
export const browser = () =>{
  var u = window.navigator.userAgent;
  return {
    trident: u.indexOf('Trident') > -1, //IE内核
    presto: u.indexOf('Presto') > -1, //opera内核
    webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
    gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
    mobile: !!u.match(/AppleWebKit.*Mobile.*/)||!!u.match(/AppleWebKit/), //是否为移动终端
    ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
    android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
    iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者安卓QQ浏览器
    iPad: u.indexOf('iPad') > -1, //是否为iPad
    webApp: u.indexOf('Safari') == -1 ,//是否为web应用程序，没有头部与底部
    weixin: u.indexOf('MicroMessenger') == -1 //是否为微信浏览器
  };
}
// 截取url 里面token的值
export const getQueryString = name => {
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  let params = window.location.search.substr(1) || window.location.href.split('?')[1]
  let r = params && params.match(reg)
  if (r != null) {
    return unescape(r[2])
  }
  return null
}

// 时间转换
export const formatTimeAmt = (fmt, msg) => {
  Date.prototype.format = function(fmt) {
    var o = {
      'M+': this.getMonth() + 1,
			'd+': this.getDate(),
			'h+': this.getHours(),
			'H+': this.getHours(),
			'm+': this.getMinutes(),
			's+': this.getSeconds(),
			'q+': Math.floor((this.getMonth() + 3) / 3),
			'S': this.getMilliseconds()
		}
		var week = {
			'0': '\u65e5',
			'1': '\u4e00',
			'2': '\u4e8c',
			'3': '\u4e09',
			'4': '\u56db',
			'5': '\u4e94',
			'6': '\u516d'
		}
		if (/(y+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
		}
		if (/(E+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f': '\u5468') : '') + week[this.getDay() + ''])
		}
		for (var k in o) {
			if (new RegExp('(' + k + ')').test(fmt)) {
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
			}
		}
		return fmt
	}
	return new Date(fmt).format(msg)
}

// splic()截取，转对象
export const getQueryJson = (name, url) => {
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  let params = url
  let r = params && params.match(reg)
  if (r != null) {
    return unescape(r[2])
  }
  return null
}
// 格式化金额
export const fixed2NoRound = (money) => {
  let newMoney = Math.ceil((money * 100).toFixed(5)) / 100
  return newMoney.toFixed(2)
}
// 数字显示*
export const getHiddenText = (text, start, end, spaceNum, spaceTxt) => {
  // text : 替换的字符串  start : 替换起点  end:替换终点 spaceNum ： 表示每几个字符一个空格
  var arryText = text.split('')
  var arryTemp = []
  var spaceCount = 0
  for (var i in arryText) {
    spaceCount++
    if (i >= start && i <= end) {
      arryTemp.push('*')
    } else {
      arryTemp.push(arryText[i])
    }
    if (spaceNum && spaceCount % spaceNum == 0) {
      spaceTxt = spaceTxt || ' '
      arryTemp.push(spaceTxt)
      spaceCount = 0
    }
  }
  return arryTemp.join('')
}
// 区分用户来源
export const userFromChannel = () => {
  const fc = getQueryString('fromChannel')
  const fromC = window.sessionStorage.getItem('fromChannel')
  let fromChannel = 'sxymh5'
  if (fc && fc!='') {
    fromChannel = fc
  } else if (/waterMall/i.test(navigator.userAgent)) {
    fromChannel = 'sxymApp'
  } else if (fromC) {
    fromChannel = fromC
  }
  return fromChannel
}
export const html_encode = (str) => {
  var s = ''
  if (str.length == 0) return ''
  s = str.replace(/&amp;/g, '&')
  s = s.replace(/&lt;/g, '<')
  s = s.replace(/&gt;/g, '>')
  s = s.replace(/&nbsp;/g, ' ')
  s = s.replace(/&#39;/g, "\'")
  s = s.replace(/&quot;/g, '"')
  s = s.replace(/<br>/g, '\n')
  return s
}
// 登录
export const jointLand = () => {
  // const fromChannel = userFromChannel()
  const callBackUrl = Base64.encode(window.location.href)
  let urls = process.env.URL
  window.location.href = `${urls}?callBackUrl=${callBackUrl}`
}
export const base64ToFile = (base64, name) => {
  let arr = base64.split(',')
  let mine = arr[0].match(/:(.*?);/)[1]
  let bstr = atob(arr[1])
  let n = bstr.length
  let u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new File([u8arr], name, {
    type: mine
  })
}
